//
// Created by nicemoe on 2021/8/18.
//

#include  <cstring>
#include "StringBad.h"

using std::cout;
using std::endl;

int StringBad::num_strings = 0;

StringBad::StringBad(const char *s) {
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    num_strings++;
    cout << num_strings << " : \"" << str << "\" object created" << endl;
}

StringBad::StringBad() {
    len = 4;
    str = new char[4];
    std::strcpy(str, "C++");
    num_strings++;
    cout << num_strings << " : \"" << str << "\" object created" << endl;
}

StringBad::~StringBad() {
    cout << "\"" << str << "\" object deleted" << endl;
    --num_strings;
    cout << num_strings << " left" << endl;
    delete[] str;
}

StringBad &StringBad::operator=(const StringBad &st) {
    if (this == &st) {
        return *this;
    }
    delete[] str;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);
    return *this;
}

std::ostream &operator<<(std::ostream &os, const StringBad &st) {
    os << st.str;
    return os;
}